package com.sxt.pro.controller;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxt.pro.pojo.ServiceLead;
import com.sxt.pro.service.ServiceLeadService;
import com.sxt.pro.vo.ServiceLeadVo;
import com.sxt.sys.common.DataGridView;
import com.sxt.sys.common.ResultObj;
import com.sxt.sys.common.WebUtils;


/**  

* @Description: 业务引导控制层

* @author xinxiaokang  

* @date 2021年3月8日 上午10:33:16

*/
@RestController
@RequestMapping("/serviceLead")
public class ServiceLeadController {
	
	@Autowired
	private ServiceLeadService serviceLeadService;
	
	/**
	 * 查询业务引导及分页列表
	 * @param serviceLeadVo
	 * @return
	 */
	@RequestMapping("/loadAllServiceLead")
	public DataGridView loadAllServiceLead(ServiceLeadVo serviceLeadVo){
		//存入网点编号和用户名放入session中
		 int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		 serviceLeadVo.setBanknbr(banknbr);
		//初始化 当前页、每页显示条数
		IPage<ServiceLead> page = new Page<>(serviceLeadVo.getPage(), serviceLeadVo.getLimit());
		//封装对象查询条件
		QueryWrapper<ServiceLead> queryWrapper = new QueryWrapper<ServiceLead>();
		//筛选条件：网点编号
		queryWrapper.eq(serviceLeadVo.getBanknbr()!=null&&serviceLeadVo.getBanknbr()!=0,"banknbr",serviceLeadVo.getBanknbr());
		//筛选条件:标题
		queryWrapper.like(StringUtils.isNotBlank(serviceLeadVo.getTitle()), "title", serviceLeadVo.getTitle());
		//筛选条件：大于开始时间
		queryWrapper.ge(serviceLeadVo.getStartTime()!=null, "createtime", serviceLeadVo.getStartTime());
		//筛选条件：小于结束时间
		queryWrapper.le(serviceLeadVo.getEndTime()!=null, "createtime", serviceLeadVo.getEndTime());
		queryWrapper.orderByDesc("createtime");
		//获取分页数据
		this.serviceLeadService.page(page, queryWrapper);
		return new DataGridView(page.getTotal(), page.getRecords());
	}
	
	/**
	 * 添加业务引导
	 * @param serviceLeadVo
	 * @return
	 */
	@RequestMapping("addServiceLead")
	public ResultObj addServiceLead(ServiceLeadVo serviceLeadVo) {
		//添加时，网点默认
		int banknbr = (int) WebUtils.getSession().getAttribute("banknbr");
		serviceLeadVo.setBanknbr(banknbr);
		try {
			serviceLeadVo.setCreatetime(new Date());
			this.serviceLeadService.save(serviceLeadVo);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.ADD_ERROR;
		}
	}

	/**
	 * 修改业务引导
	 * @param serviceLeadVo
	 * @return
	 */
	@RequestMapping("updateServiceLead")
	public ResultObj updateServiceLead(ServiceLeadVo serviceLeadVo) {
		try {
			this.serviceLeadService.updateById(serviceLeadVo);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.UPDATE_ERROR;
		}
	}

	/**
	 * 删除业务引导
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteServiceLead")
	public ResultObj deleteServiceLead(Integer id,String producttypeimage) {
		try {
			this.serviceLeadService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ResultObj.DELETE_ERROR;
		}
	}
	
	
}
